package pushtest.mobike.com.mtpush

import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.view.ViewStub
import android.widget.Toast
import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.ObservableEmitter
import pushtest.mobike.com.mtpush.databinding.ActivityMainBinding
import pushtest.mobike.com.mtpush.di.component.AppComponent
import pushtest.mobike.com.mtpush.di.component.DaggerAppComponent
import pushtest.mobike.com.mtpush.di.component.DaggerMainActivityComponent
import pushtest.mobike.com.mtpush.di.module.ActivityModule
import pushtest.mobike.com.mtpush.di.module.AppModule
import pushtest.mobike.com.mtpush.viewModel.ViewModelMain
import timber.log.Timber
import java.util.*
import java.util.concurrent.TimeUnit
import javax.inject.Inject

class MainActivity : AppCompatActivity() {

  @Inject
  lateinit var viewModel: ViewModelMain

  var dataBinding: ActivityMainBinding? = null
  override fun onCreate(savedInstanceState: Bundle?) {
    val build = DaggerMainActivityComponent.builder()
      .activityModule(ActivityModule())
      .appComponent(DaggerAppComponent.builder().appModule(AppModule(this)).build())
      .build().inject(this)
    super.onCreate(savedInstanceState)
    dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    dataBinding?.navigation?.setOnNavigationItemSelectedListener(createListener())
    dataBinding?.vm = viewModel
  }

  fun createListener() =
    BottomNavigationView.OnNavigationItemSelectedListener { item ->
      when (item.itemId) {
        R.id.navigation_home -> {
          viewModel.updateString(R.string.title_home)
          testObservable()
          return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_dashboard -> {
          testRun()
          viewModel.updateString(R.string.title_dashboard)
          dataBinding?.test?.let {
            it.viewStub?.visibility = View.VISIBLE
            it.setOnInflateListener { stub: ViewStub?, inflated: View? ->
              Log.e("Main", "sub = $stub  inflate = $inflated")
            }
          }
          return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_notifications -> {
          viewModel.updateString(R.string.title_notifications)
          return@OnNavigationItemSelectedListener true
        }
      }
      false
    }

  fun testObservable() {
    var e: ObservableEmitter<Integer>? = null
    val ob = Observable.create<Integer> { emitter ->
      e = emitter
    }.publish()
    ob.connect()
//    val ob = Observable.just(1,2,3,5)
    ob.subscribe { Log.e("TARGEt", "value 1 = $it") }

    ob.subscribe { Log.e("TARGEt", "value 2= $it") }



    for (i in 1..5) {
      e?.onNext(Random().nextInt(i) as Integer)
    }


  }

  private fun testRun() {
    Observable.interval(3, TimeUnit.SECONDS, io.reactivex.schedulers.Schedulers.io())
      .map {
        Timber.e(it.toString())
        it
      }
      .take(6, TimeUnit.SECONDS)
      .map {
        Timber.e(it.toString())
        it
      }
      .first(-1)
//      .run {
//        first(-1)
//      }
      .doOnSuccess {
        Timber.e(it.toString())
      }.flatMapCompletable {
        Timber.e("flatMapCompletable $it")
        Completable.complete()
      }.doOnComplete {
        Log.e("TARGEt", "done")

        Timber.e("done")
      }.subscribe()

  }

}
